package cn.cc.sort;

import java.util.ArrayList;

public class Matrix {
    public static void main(String[] args) {
        int [][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
        System.out.println(print(matrix));
    }
    public static ArrayList<Integer> print(int [][] matrix) {

        ArrayList<Integer> result = new ArrayList<Integer>();
        if(matrix == null){
            return result;
        }

        int length = matrix.length;
        int n = 0;
        if(length > 0){
            n = matrix[0].length;
        }
        for(int j = 0; j < n; j++){
            result.add(matrix[0][j]);
        }
        if(length == 1){
            return result;
        }else{
            int[][] mat = new int[length -1][n];

            for(int i = 1; i < length ; i++){
                for(int j = 0; j < n; j++){
                    mat[i-1][j] = matrix[i][j];
                }
            }

            result.addAll(print(rotateMatrix(mat, length -1, n)));
        }

        return result;

    }

    public static int[][] rotateMatrix(int[][] mat, int m, int n) {
        //将一个二维数组进行逆时针旋转90
        if(mat == null){
            return null;
        }
        int[][] rmat = new int[n][m];
        for(int i = 0; i< n; i++){
            for(int j = 0; j < m; j++){
                rmat[i][j] = mat[j][n-i-1];
            }
        }

        return rmat;
    }
}